package com.ouc.dataMange.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ouc.common.config.OUCAjaxResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ouc.common.annotation.Log;
import com.ouc.common.core.controller.BaseController;
import com.ouc.common.core.domain.AjaxResult;
import com.ouc.common.enums.BusinessType;
import com.ouc.dataMange.domain.SysDatamange;
import com.ouc.dataMange.service.ISysDatamangeService;
import com.ouc.common.utils.poi.ExcelUtil;
import com.ouc.common.core.page.TableDataInfo;

/**
 * dataMangeController
 *
 * @author ouc
 * @date 2025-07-16
 */
@RestController
@RequestMapping("/dataMange/datamange")
public class SysDatamangeController extends BaseController {
  @Autowired
  private ISysDatamangeService sysDatamangeService;

  /**
   * 查询dataMange列表
   */
  @PreAuthorize("@ss.hasPermi('dataMange:datamange:list')")
  @GetMapping("/list")
  public TableDataInfo list(SysDatamange sysDatamange) {
    startPage();
    List<SysDatamange> list = sysDatamangeService.selectSysDatamangeList(sysDatamange);
    return getDataTable(list);
  }

  /**
   * 导出dataMange列表
   */
  @PreAuthorize("@ss.hasPermi('dataMange:datamange:export')")
  @Log(title = "dataMange", businessType = BusinessType.EXPORT)
  @PostMapping("/export")
  public void export(HttpServletResponse response, SysDatamange sysDatamange) {
    List<SysDatamange> list = sysDatamangeService.selectSysDatamangeList(sysDatamange);
    ExcelUtil<SysDatamange> util = new ExcelUtil<SysDatamange>(SysDatamange.class);
    util.exportExcel(response, list, "dataMange数据");
  }

  /**
   * 获取dataMange详细信息
   */
  @PreAuthorize("@ss.hasPermi('dataMange:datamange:query')")
  @GetMapping(value = "/{id}")
  public AjaxResult getInfo(@PathVariable("id") Integer id) {
    return success(sysDatamangeService.selectSysDatamangeById(id));
  }

  /**
   * 新增dataMange
   */
  @PreAuthorize("@ss.hasPermi('dataMange:datamange:add')")
  @Log(title = "dataMange", businessType = BusinessType.INSERT)
  @PostMapping
  public AjaxResult add(@RequestBody SysDatamange sysDatamange) {
    return toAjax(sysDatamangeService.insertSysDatamange(sysDatamange));
  }

  /**
   * 修改dataMange
   */
  @PreAuthorize("@ss.hasPermi('dataMange:datamange:edit')")
  @Log(title = "dataMange", businessType = BusinessType.UPDATE)
  @PutMapping
  public AjaxResult edit(@RequestBody SysDatamange sysDatamange) {
    return toAjax(sysDatamangeService.updateSysDatamange(sysDatamange));
  }

  /**
   * 删除dataMange
   */
  @PreAuthorize("@ss.hasPermi('dataMange:datamange:remove')")
  @Log(title = "dataMange", businessType = BusinessType.DELETE)
  @DeleteMapping("/{ids}")
  public AjaxResult remove(@PathVariable Long[] ids) {
    return toAjax(sysDatamangeService.deleteSysDatamangeByIds(ids));
  }

  /**
   * 获取数据类型树状列表
   */
  @ResponseBody
  @RequestMapping("/getDataMangeTreeList")
  public OUCAjaxResult getDataMangeTreeList(SysDatamange sysDatamange) {
    List<SysDatamange> datamangeList = sysDatamangeService.selectSysDatamangeList(sysDatamange);
    List<SysDatamange> datamangeResult = new ArrayList<>();
    for (int i = 0; i < datamangeList.size(); i++) {
      if (datamangeList.get(i).getParentId() == null) {
        SysDatamange datamange = datamangeList.get(i);
        SysDatamange sysDatamangeSelect = new SysDatamange();
        sysDatamangeSelect.setParentId(datamange.getId());
        datamange.setChildren(sysDatamangeService.selectSysDatamangeList(sysDatamangeSelect));
        datamangeResult.add(datamange);
      }
    }
    return OUCAjaxResult.ok(datamangeResult);
  }

}
